﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Samples.BareMinimum.Areas.Services.Models
{
    public class Product
    {
        public string Uri { get; set; }
        public int Id { get; set; }
        public string Name { get; set; }
        public Decimal? UnitPrice { get; set; }
        public int UnitsInStock { get; set; }

        private static ProductCollection _allProducts;
        public static ProductCollection AllProducts
        {
            get
            {
                return _allProducts ?? (_allProducts = new ProductCollection
                                                       {
                                                          new Product {Name = "Coke", Id = 0, UnitPrice = 1.25M, UnitsInStock = 101},
                                                          new Product {Name = "Flank Steak", Id = 1, UnitPrice = 35.45M, UnitsInStock = 2},
                                                          new Product {Name = "Cheerios", Id = 2, UnitPrice = 3.45M, UnitsInStock = 88}
                                                       });
            }
        }
    }

    public class ProductCollection : KeyedCollection<int, Product>
    {
        protected override int GetKeyForItem(Product item)
        {
            return item.Id;
        }
    
        // This method is included as a workaround to an issue in the XmlSerializer
        // for more information see: http://www.pluralsight.com/blogs/craig/archive/2005/10/19/15696.aspx
        public new Product this[int index]
        {
            get { return ((IList<Product>)this)[index]; }
        }
    }

}